home *** CD-ROM | disk | FTP | other *** search
- /* pixmap.c
- * This program renders a scene to either a pixmap or a window.
- * The scene from the pixmap can be copied forward the to window.
- * The scenes are slightly different so that is clear when the
- * pixmap is being copied, and when the program is rendering directly
- * to the window.
- *
- * Original Author: Lesley Kalmin, SGI
- *
- * Hit p to render to a pixmap, w to render to a window, and c to copy
- * forward from the pixmap to the window.
- */
-
- /*
- ** $Revision: 1.2 $
- ** $Date: 1996/07/09 23:19:13 $
- */
- #include <GL/glx.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <X11/keysym.h>
-
- static int RGBattributes[] = {
- GLX_RGBA,
- GLX_RED_SIZE, 1,
- GLX_GREEN_SIZE, 1,
- GLX_BLUE_SIZE, 1,
- None,
- };
-
- static int CIattributes[] = {
- GLX_DEPTH_SIZE, 1,
- None,
- };
-
- int width = 200, height = 200;
-
- /* render an extra triangle to the pixmap
- * so that we can tell the difference between
- * the scene rendered to the window and the
- * one copied from the pixmap
- */
- GLboolean render_to_pixmap = GL_TRUE;
-
- static void Reshape(int w, int h)
- {
- glViewport(0, 0, (GLint)w, (GLint)h);
-
- width = w;
- height = h;
-
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho(-5.0, 5.0, -5.0, 5.0, -5.0, 5.0);
- glMatrixMode(GL_MODELVIEW);
- }
-
-
- static void Init(void)
- {
-
- glClearColor(0.0, 0.0, 0.0, 0.0);
- glClearIndex(1.0);
-
- glClearDepth(1);
- glClearStencil(0);
- glEnable(GL_DEPTH_TEST);
- }
-
-
-
- static void DoDisplay(void)
- {
- glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
-
- glColor3ub(200, 0, 0);
- glIndexi(2);
- glBegin(GL_TRIANGLES);
- glVertex3i(-4, -3, -2);
- glVertex3i( 4, -3, -2);
- glVertex3i( 0, 4, -2);
- glEnd();
-
- glEnable(GL_SCISSOR_TEST);
- glScissor(width/3, height/3, width/3, height/3);
- glColor3ub(0, 200, 0);
- glIndexi(29);
- glBegin(GL_TRIANGLES);
- glVertex3i(-4, -3, 0);
- glVertex3i( 4, -3, 0);
- glVertex3i( 0, 4, 0);
- glEnd();
- glDisable(GL_SCISSOR_TEST);
-
- if (render_to_pixmap) {
- glColor3ub(0, 0, 200);
- glIndexi(31);
- glBegin(GL_TRIANGLES);
- glVertex3i(-4, -4, -1);
- glVertex3i( 4, -4, -1);
- glVertex3i( 0, 3, -1);
- glEnd();
- }
-
- }
-
- static Bool WaitForMapNotify(Display *d, XEvent *e, char *arg)
- {
- if ((e->type == MapNotify) && (e->xmap.window == (Window)arg)) {
- return GL_TRUE;
- }
- return GL_FALSE;
- }
-
- int main(int argc, char *argv[])
- {
- XVisualInfo *vi;
- Display *dpy;
- Colormap cmap;
- Window window;
- XSetWindowAttributes swa;
- GLXContext cx;
- XEvent event;
- GLboolean needDisplay;
- Pixmap pixmap;
- GLXPixmap pm;
- int gx, gy;
- Window root;
- unsigned int gwidth, gheight, gborder_width, gdepth;
- Status xggStatus;
- GC gc;
- int rgb = 1, i;
- rgb = 1;
- for (i = 1; i < argc; i++) {
- if (argv[i][0] == '-') {
- switch (argv[i][1]) {
- case 'c':
- rgb = 0;
- break;
- }
- }
- }
-
- dpy = XOpenDisplay(0);
- if (!dpy) {
- fprintf(stderr, "Can't connect to display \"%s\"\n",
- getenv("DISPLAY"));
- return -1;
- }
-
- vi = glXChooseVisual(dpy, DefaultScreen(dpy),
- rgb ? RGBattributes : CIattributes);
- if (!vi) {
- fprintf(stderr, "No singlebuffered rgba visual on \"%s\"\n",
- getenv("DISPLAY"));
- return -1;
- }
-
- cmap = XCreateColormap(dpy, RootWindow(dpy, vi->screen), vi->visual,
- rgb ? AllocNone: AllocAll);
- /* XXX
- ** could use some colormap setting here.
- */
-
- swa.border_pixel = 0;
- swa.colormap = cmap;
- swa.event_mask = ExposureMask | StructureNotifyMask | KeyPressMask
- | KeyReleaseMask;
- window = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 10, 10,
- width, height,
- 0, vi->depth, InputOutput, vi->visual,
- CWBorderPixel|CWColormap|CWEventMask, &swa);
- XSetStandardProperties(dpy, window, "pixmap", "pixmap", None, argv,
- argc, NULL);
- XSetWMColormapWindows(dpy, window, &window, 1);
- XMapWindow(dpy, window);
- XIfEvent(dpy, &event, WaitForMapNotify, (char*)window);
- gc = XCreateGC(dpy, window, 0, NULL);
-
- cx = glXCreateContext(dpy, vi, 0, GL_FALSE);
- if (!glXMakeCurrent(dpy, window, cx)) {
- fprintf(stderr, "Can't make window current to context\n");
- return -1;
- }
- glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
-
- /******* Create Pixmap */
- pixmap = XCreatePixmap(dpy, RootWindow(dpy, vi->screen),
- width, height, vi->depth);
- pm = glXCreateGLXPixmap(dpy, vi, pixmap);
- xggStatus = XGetGeometry(dpy, pixmap, &root, &gx, &gy, &gwidth,
- &gheight, &gborder_width, &gdepth);
-
- if (!glXMakeCurrent(dpy, pm, cx)) {
- fprintf(stderr, "Can't make pixmap current to context\n");
- return -1;
- }
- printf("rendering to pixmap\n");
-
- Init();
-
- printf("Hit p to render to a pixmap.\n");
- printf("Hit w to render to a window.\n");
- printf("Hit c to copy forward from the pixmap to the window.\n");
-
- needDisplay = GL_TRUE;
- for (;;) {
- do {
- XNextEvent(dpy, &event);
- switch (event.type) {
- case Expose:
- needDisplay = GL_TRUE;
- break;
- case ConfigureNotify:
- width = event.xconfigure.width;
- height = event.xconfigure.height;
- Reshape(width, height);
- needDisplay = GL_TRUE;
- break;
- case KeyPress:
- {
- char buf[100];
- int rv;
- KeySym ks;
-
- rv = XLookupString(&event.xkey, buf, sizeof(buf), &ks, 0);
- switch (ks) {
- case XK_c:
- xggStatus = XGetGeometry(dpy, window, &root, &gx, &gy,
- &gwidth, &gheight, &gborder_width, &gdepth);
- XCopyArea(dpy, pixmap, window, gc, 0, 0, gwidth,
- gheight, gx, gy);
- printf("copying pixmap to window\n");
- break;
- case XK_p:
- glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
- if (!glXMakeCurrent(dpy, pm, cx)) {
- fprintf(stderr,
- "Can't make pixmap current to context\n");
- return -1;
- }
- render_to_pixmap = GL_TRUE;
- needDisplay = GL_TRUE;
- printf("rendering to pixmap\n");
- break;
- case XK_w:
- if (!glXMakeCurrent(dpy, window, cx)) {
-
- fprintf(stderr,
- "Can't make window current to context\n");
- return -1;
- }
- render_to_pixmap = GL_FALSE;
- needDisplay = GL_TRUE;
- printf("rendering to window\n");
- break;
- case XK_Escape:
- return 0;
- }
- }
- break;
- }
- } while (XPending(dpy) != 0);
-
- if (needDisplay) {
- needDisplay = GL_FALSE;
- DoDisplay();
- glFlush();
- }
- }
- }
-